var log;
var httpClient;
var dirName;
var separator = java.io.File.separator;
var Jsoup = Java.type('org.jsoup.Jsoup');
var JSONObject = Java.type('com.ssy.utils.http.MyJsonObject');
var JSON = Java.type('com.alibaba.fastjson.JSON');
var JSONArray = Java.type('com.alibaba.fastjson.JSONArray');
var taskKey;
function setLog(logg){
	log=logg
}


function LinkedHashMap() {
	return new java.util.LinkedHashMap();
}
function LinkedHashSet() {
	return new java.util.LinkedHashSet();
}

function parseObject(jsonStr) {
	return com.alibaba.fastjson.JSON.parseObject(jsonStr);
}

String.prototype.contains = function(str) {
	if (str == undefined || str == null || typeof (str) != 'string') {
		return false;
	} else {
		var index = this.indexOf(str);
		if (index >= 0) {
			return true;
		} else {
			return false;
		}
	}
}
String.prototype.equals = function(str) {
	if (str == undefined || str == null || typeof (str) != 'string') {
		return false;
	} else {
		return this == str;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/gm, '');
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.format = function(fmt) { // author: xuyiming
	if (fmt == undefined) {
		fmt = "yyyy-MM-dd";
	}
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	// 毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

/**
 * 将1,234,567.00转换为1234567.00<br>
 * 1+100 显示101
 * 
 * @param val
 * @returns
 */
function moneyToNumValue(val) {
	if (val == undefined || val == null || typeof (val) != 'string') {
		return 0;
	}
	var ss = val.trim();
	if (ss.length == 0) {
		return 0;
	}
	var c = ss.split("+");
	if (c.length > 1) {
		var sum = 0;
		for ( var i in c) {
			sum = sum + moneyToNumValue(c[i])
		}
		return sum;
	}
	return parseInt(ss.replace(/,/g, ""));
}

function write(filename, data) {
	var c = data;
	if (data == undefined || data == null) {
		c = "";
	} else {
		if (typeof (data) != 'string') {
			c = data.toString();
		}
	}
	var f = new java.io.File(dirName);
	if (!f.exists()) {
		f.mkdirs();
	}
	var fileName = dirName + separator + filename;
	log.info("write to file : {}", fileName)
	org.apache.commons.io.FileUtils.write(new java.io.File(fileName), c, "utf-8");
}

function readFileToString(path, charset) {
	if (isBlank(charset)) {
		charset = "utf-8"
	}
	var file = new java.io.File(path);
	var text = org.apache.commons.io.FileUtils.readFileToString(file, charset);
	return text;
}

function StringBuffer() {
	return new java.lang.StringBuffer();
}

/**
 * 日期相加操作
 * 
 * @param date
 *            日期
 * @param unit
 *            单位 y,m,d
 * @param num
 *            日期改变的数量
 * @returns {Date}
 */
function addDate(date, unit, num) {
	var d = new Date(date);

	switch (unit) {
	case 'd':
		d.setDate(d.getDate() + num);
		break;
	case 'm':
		d.setMonth(d.getMonth() + num)
		break;
	case 'y':
		d.setYear(d.getFullYear() + num)
		break;
	}
	return d;
}

function getLastMonthArray(num, date, unit) {
	if (num > 0) {
		num = -num;
	}
	if (date == undefined) {
		date = new Date();
	}
	if (unit == undefined) {
		unit = "m";
	}
	var newDate = new Date(date);
	newDate = addDate(newDate, unit, num);
	var startyear = newDate.getFullYear();
	var endyear = date.getFullYear();
	var startMonth = newDate.getMonth() + 1;
	var endMonth = date.getMonth() + 1;
	var a = new Array();
	for (var j = 0; j <= endyear + 1 - startyear; j++) {
		if (j > 0) {
			startMonth = 1
		}
		startyear = startyear + j;
		for (var i = 0; startyear * 100 + startMonth + i <= endyear * 100 + endMonth && startMonth + i <= 12; i++) {
			a.push(startyear * 100 + startMonth + i);
		}
	}
	return a;
}

function getLastYearArray(num, date, unit) {
	if (num > 0) {
		num = -num;
	}
	if (date == undefined) {
		date = new Date();
	}
	if (unit == undefined) {
		unit = "m";
	}
	var newDate = new Date(date);
	newDate = addDate(newDate, unit, num);
	var startyear = newDate.getFullYear();
	var endyear = date.getFullYear();
	var a = new Array();
	for (var j = 0; j < endyear + 1 - startyear; j++) {
		a.push(startyear + j);
	}
	return a;
}

/**
 * 检查Java中map<string,string>的类型
 * 
 * @param postdata
 */
function checkJavaMap(postdata) {
	if (postdata == undefined || postdata == null) {
		postdata = null;
	} else {
		if (!(postdata instanceof java.util.Map)) {
			log.error(postdata)
			throw new java.lang.Exception("header 对象类型不对");
		}
	}
	for ( var i in postdata) {
		if (postdata.get(i) == undefined) {
			log.error(i + " is undefined , set it is null")
			postdata.put(i, "")
		}
	}
	return postdata;
}

function setHttpClient(hc) {
	if (hc == undefined || !(hc instanceof com.ssy.utils.http.EasyHttpClient)) {
		log.error("httpclient 对象为空")
		throw new java.lang.Exception("httpclient 对象为空");
	}
	log.info("设置httpclient {}", hc)
	httpClient = hc;
}

function setProxy(host, port) {
	if (host != undefined && typeof (host) != 'string' && port != undefined && typeof (port) == 'number') {
		log.info("设置httpclient代理 {}:{}", host, port)
		httpClient.setProxy(host, port);
	} else {
		log.info("设置httpclient代理 失败")
	}
}

function setTaskKey(_taskKey) {
	if (_taskKey == undefined || _taskKey == null) {
		throw new java.lang.Exception("taskKey 为空");
	}
	log.info("taskKey {}", _taskKey)
	taskKey = _taskKey;
}

function setFile(javadirName) {
	log.info("设置目录名称 " + javadirName)
	dirName = javadirName;
}

/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是对象
 * 
 * @param posturl
 * @param header
 * @param postdata
 * @param charset
 */
function postFormData(posturl, header, postdata, charset) {
	if (httpClient == undefined || !(httpClient instanceof com.ssy.utils.http.EasyHttpClient)) {
		log.error("httpclient 对象为空")
		throw new java.lang.Exception("httpclient 对象为空");
	}
	if (posturl == undefined || typeof (posturl) != 'string') {
		log.error("posturl 对象为空")
		throw new java.lang.Exception("posturl 对象为空");
	}
	if (charset == undefined || typeof (charset) != 'string') {
		charset = "";
	}
	header = checkJavaMap(header);
	postdata = checkJavaMap(postdata)

	var c = httpClient.postFormData(posturl, header, postdata, charset);
//	write(new Date().valueOf() + ".html", c)
	return c;
}

/**
 * 设置是否手动302
 * 
 * @param status
 */
function setShouDong302(status) {
	httpClient.setShouDong302(status);
}
/**
 * 设置是否分块传输
 * 
 * @param status
 */
function setChunk(status) {
	httpClient.setChunk(status);
}

/**
 * 全局header,优先级大于默认，小于最新设置的,不考虑key的大小写
 * 
 * @param key
 * @param value
 */
function putWholeHeader(key, value) {
	httpClient.putWholeHeader(key, value);
}

/**
 * 获得http异常，一般是网站超时，unknown host等
 * 
 * @return
 */
function getErrorMsg() {
	var map = httpClient.getErrorExceptionMsg();
	var sb = new StringBuffer();
	if (map != null) {
		for (var string in map.keySet()) {
			sb.append(map.get(string) + " \t");
		}
	}
	return sb.toString();
}
/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是网页
 * 
 * @param posturl
 * @param header
 * @param postdata
 * @returns {*}
 */
function postFormDataHtml(posturl, header, postdata) {
	return postFormData(posturl, header, postdata, "").getHtml();
}

/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是网页
 * 
 * @param posturl
 * @param header
 * @param postdata
 * @returns
 */
function Post(posturl, header, postdata) {
	return postFormData(posturl, header, postdata, "").getHtml();
}

/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是对象
 * 
 * @param posturl
 * @param header
 * @param json
 * @param charset
 */
function postJsonStr(posturl, header, json, charset) {
	if (httpClient == undefined || !(httpClient instanceof com.ssy.utils.http.EasyHttpClient)) {
		log.error("httpclient 对象为空")
		throw new java.lang.Exception("httpclient 对象为空");
	}
	if (posturl == undefined || typeof (posturl) != 'string') {
		log.error("posturl 对象为空")
		throw new java.lang.Exception("posturl 对象为空");
	}
	if (json == undefined || typeof (json) != 'string') {
		log.error("json 对象为空,设置为{}")
		json = "{}";
	}

	header = checkJavaMap(header);
	var c = httpClient.postJsonStr(posturl, header, json);
//	write(new Date().valueOf() + ".html", c)
	return c;
}

/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是对象
 * 
 * @param url
 * @param header
 */
function getBytes(url, header) {
	if (httpClient == undefined || !(httpClient instanceof com.ssy.utils.http.EasyHttpClient)) {
		log.error("httpclient 对象为空")
		throw new java.lang.Exception("httpclient 对象为空");
	}
	if (url == undefined || typeof (url) != 'string') {
		log.error("posturl 对象为空")
		throw new java.lang.Exception("posturl 对象为空");
	}
	header = checkJavaMap(header);

	var c = httpClient.getBytes(url, header);
//	write(new Date().valueOf() + ".html", c)
	return c;
}
function GetImg(url, header) {
	return getBytes(url, header).getBytes();
}

/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是字符串
 * 
 * @param url
 * @param header
 * @param charset
 * @returns
 */
function Get(url, header, charset) {
	if (httpClient == undefined || !(httpClient instanceof com.ssy.utils.http.EasyHttpClient)) {
		log.error("httpclient 对象为空")
		throw new java.lang.Exception("httpclient 对象为空");
	}
	if (url == undefined || typeof (url) != 'string') {
		log.error("posturl 对象为空")
		throw new java.lang.Exception("posturl 对象为空");
	}
	if (charset == undefined || typeof (charset) != 'string') {
		charset = "";
	}
	header = checkJavaMap(header);
	var c = httpClient.Get(url, header, charset);
	//write(new Date().valueOf() + ".html", c)
	return c;
}
/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是字符串
 * 
 * @param url
 * @returns
 */
function getHtml(url) {
	return Get(url, null, "");
}

/**
 * 通过Java的httpclient提交参数<br>
 * 返回值是对象
 * 
 * @param url
 * @param header
 * @param charset
 * @returns
 */
function get(url, header, charset) {
	if (httpClient == undefined || !(httpClient instanceof com.ssy.utils.http.EasyHttpClient)) {
		log.error("httpclient 对象为空")
		throw new java.lang.Exception("httpclient 对象为空");
	}
	if (url == undefined || typeof (url) != 'string') {
		log.error("posturl 对象为空")
		throw new java.lang.Exception("posturl 对象为空");
	}
	if (charset == undefined || typeof (charset) != 'string') {
		charset = "";
	}
	header = checkJavaMap(header);
	var c = httpClient.get(url, header, charset);
//	write(new Date().valueOf() + ".html", c)
	return c;
}

function format(str, arr) {
	if (typeof (str) == 'string') {
		if (arguments.length > 1) {
			var c = new Array();
			for ( var i in arguments) {
				if (i > 0) {
					c.push(arguments[i])
				}
			}
			return java.lang.String.format(str, c);
		} else {
			return str;
		}
	}
	return "";
}

function GetText(str, reg) {
	return com.ssy.utils.http.RegexUtils.GetText(str, reg);
}

function isBlank(str) {
	if (str == undefined) {
		return true;
	}
	return org.apache.commons.lang3.StringUtils.isBlank(str);
}

function getCode(bytes) {
	if (taskKey == undefined) {
		taskKey = "123456";
	}
	var c = com.ssy.utils.http.image.ImageCodeUtil.getCode(bytes, taskKey);
	return c;
}

function parse(html) {
	return Jsoup.parse(html);
}

function select(doc, css) {
	var a = new Array();
	var e = doc.select("a");
	for (var int1 = 0; int1 < e.size(); int1++) {
		a.push(e.get(int1))
	}
	return a;
}

function test() {
	var a = "11111"
	print("test")
}


function loadJs(jsPath) {
	load(jsPath)
}